dojo.provide("lorelei.articles");
// Lorelei articles view page script
// Copyright (c)2008 Logue <http://logue.be/> All rights reserved.

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

lorelei.autolink = {
	meta: {
		"@prefix": "<http://purl.org/net/ns/doas#>",
		"@about": "<http://mabinogi.logue.be/js/lolerei/articles.js>", a: ":JavaScript",
		 title: "Lorelei Article page script",
		 created: "2008-12-06", release: {revision: "1.0", created: "2008-12-06"},
		 author: {name: "Logue", homepage: "<http://logue.be/>"},
		 license: "<http://www.gnu.org/licenses/gpl-3.0.html>"
	},
	replace : function(str){
		var regURL = new RegExp("(http|https)://([-/.a-zA-Z0-9_~#%$?&=:200-377()]+)","gi");
		var regEmail = new RegExp("([xA1-xFEa-z0-9_-]+@[xA1-xFEa-z0-9-]+\.[a-z0-9-]+)","gi");
		
		if (str.match(/^(https?|ftp)(:\/\/[-_.!~*\'()a-zA-Z0-9;\/?:\@&=+\$,%#]+)$/'){
			switch (RegExp.$2) {
				
				default : 
					str.replace.(regURL,'<a href="javascript:lorelei.autolink.confirm('+RegExp.$1+'://'+$2+');">'+RegExp.$1+'://'+RegExp.$2+'</a>');
				break;
			}
		
		str.replace(regEmail,"<a href='mailto:$1'>$1</a>");
		return str;
	},
	confirm : function(url){
		var uri = this.referer_delater(url);
		var confirm_dialog = new dijit.Dialog({ 
			title: "Link to other Site",
			execute : function()  {
				this.hide();  // ボタンがクリックされたらダイアログを消去
			},
			content: 
				'<p>下記アドレスにリンクしようとしています。よろしければリンクをクリックしててください。</p>' +
				'<p><a href="' + uri + ' target="_blank">' + url + '</a></p>'
		});
		dojo.body().appendChild(confirm_dialog.domNode);
		confirm_dialog.startup();
	}
	referer_delater : function(uri){
		// encodeURIComponent命令使うため変換し直し（汗
		uri = uri.replace(/&#37;/g,"%");
		uri = encodeURIComponent(uri)
		if (isMSIE){
			// IEの場合、location.replaeでリファラー消えるからいいや
			uri = "javascript:location.href='" + uri + "';";
		}else{
			
			// IE以外の場合、dataスキーマーでリファラーを消すためのページを偽造
			uri = "data:text/html;charset=utf-8,%3Chtml%3E%0D%0A%3Cscript%20%3E%0D%0Alocation.replace(%22" + uri + "%22)%3B%0D%0A%3C%2Fscript%3E%0D%0A%3C%2Fhtml%3E%0D%0A";
		}
		return uri;
	}
}

